<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日程标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入日程标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日程类型" prop="datesType">
        <el-select v-model="queryParams.datesType">
          <el-option
            v-for="dict in dict.type.schedule_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择日程开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择日程结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['management:mine:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['management:mine:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['management:mine:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['management:mine:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="日程标题" align="center" prop="title"/>
      <el-table-column label="日程描述" align="center" prop="description"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}  {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}  {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="datesType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.schedule_type" :value="scope.row.datesType"/>
        </template>
      </el-table-column>
      <el-table-column label="提醒" align="center" prop="remindType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reminder_type"
                    :value="scope.row.remindType === null ? 3 : scope.row.remindType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}  {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}  {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-show="scope.row.datesType !== 0"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['management:mine:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-show="scope.row.datesType !== 0"
            @click="handleDelete(scope.row)"
            v-hasPermi="['management:mine:remove']"
          >删除
          </el-button>
          <el-button
            v-show="scope.row.datesType === 0"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['management:manage:view']"
          >查看会议
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我的日程对话框 -->
    <el-dialog v-bind="$attrs" v-on="$listeners" :visible.sync="open" width="600px" append-to-body :title="title">
      <el-form ref="formData" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-row :gutter="15">
          <el-form-item label="日程标题" prop="title">
            <el-input v-model="formData.title" placeholder="请输入日程标题" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="日程描述" prop="description">
            <el-input v-model="formData.description" placeholder="请输入日程描述" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker clearable
                            v-model="formData.startTime"
                            type="datetime"
                            :style="{width: '100%'}"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker clearable
                            v-model="formData.endTime"
                            :style="{width: '100%'}"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="日程类型" prop="datesType">
            <el-select v-model="formData.datesType" placeholder="请选择日程类型" clearable :style="{width: '100%'}">
              <el-option
                v-for="dict in dict.type.schedule_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--    查看会议对画框-->
    <el-dialog v-bind="$attrs" v-on="$listeners" :visible.sync="openView" title="查看会议信息">
      <el-row :gutter="15">
        <el-form ref="viewFrom" :model="formView" size="medium" :disabled="true"
                 label-width="101px">
          <el-col :span="24">
            <el-form-item label="会议发起人">
              <el-input v-model="formView.nickName" readonly :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议名称">
              <el-input v-model="formView.meetingName" readonly :disabled='true' :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-input v-model="formView.sex" readonly :disabled='true' :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-input v-model="formView.phoneNumber" readonly :disabled='true' :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议开始时间">
              <el-time-picker v-model="formView.startTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                              :style="{width: '100%'}"
                              :disabled='true'></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议发起时间">
              <el-date-picker type="datetime" v-model="formView.meetingLaunchDate" format="yyyy-MM-dd HH:mm:ss"
                              value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" readonly :disabled='true'>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议密码">
              <el-input v-model="formView.password" readonly :disabled='true' :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="closeView">取消</el-button>
        <el-button type="primary" @click="handleConfirmView">确定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listMine, getMine, delMine, addMine, updateMine, getMeetingById} from "@/api/management/mine"

export default {
  name: "Mine",
  dicts: ['schedule_type', 'reminder_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的日程表格数据
      mineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        datesType: '',
        startTime: null,
        endTime: null,
      },
      // 表单参数
      formData: {
        id: null,
        title: undefined,
        description: undefined,
        startTime: '',
        endTime: '',
        datesType: undefined,
      },
      // 表单校验
      rules: {
        title: [{
          required: true,
          message: '请输入日程标题',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入日程描述',
          trigger: 'blur'
        }],
        startTime: [{
          required: true,
          message: '请选择开始时间',
          trigger: 'change'
        }],
        endTime: [{
          required: true,
          message: '请选择结束时间',
          trigger: 'change'
        }],
        datesType: [{
          required: true,
          message: '请选择日程类型',
          trigger: 'change'
        }],
      },
      // 查看会议数据
      formView: {
        nickName: undefined,
        sex: undefined,
        phoneNumber: undefined,
        startDate: null,
        startTime: null,
        meetingName: undefined,
        meetingLaunchDate: null,
        password: undefined,
      },
      // 查看会议数据
      openView: false,
      elRow: '',
      elRows: '',
      isAdd: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询我的日程列表 */
    getList() {
      this.loading = true
      listMine(this.queryParams).then(response => {
        this.mineList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 显示会议数据
    handleView(row) {
      this.formView = {
        nickName: undefined,
        sex: undefined,
        phoneNumber: undefined,
        startDate: null,
        startTime: null,
        meetingName: undefined,
        meetingLaunchDate: null,
        password: undefined,
      }
      this.openView = true
      getMeetingById(row.meetingId).then(response => {
        this.formView = response.data
        this.formView.sex = this.formView.sex === '0' ? '男' : this.formView.sex === '1' ? '女' : '未知'
      })
    },
    closeView() {
      this.formView = {
        nickName: undefined,
        sex: undefined,
        phoneNumber: undefined,
        startDate: null,
        startTime: null,
        meetingName: undefined,
        meetingLaunchDate: null,
        password: undefined,
      }
      this.openView = false
    },
    handleConfirmView() {
      this.openView = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formData = {
        id: null,
        title: undefined,
        description: undefined,
        startTime: '',
        endTime: '',
        datesType: undefined,
      }
      this.resetForm("formData")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.elRows = selection.map(item => item.datesType)
      this.elRow = this.elRows[0]
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.isAdd = true
      this.open = true
      this.title = "添加我的日程"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMine(id).then(response => {
        this.formData = response.data
        this.dict.type.schedule_type.forEach(item => {
          if (item.value == this.formData.datesType) {
            this.formData.datesType = item.label
          }
        })
        this.open = true
        this.title = "修改我的日程"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          if (!this.isAdd) {
            // 做会议检测
            if (this.elRow === 0) {
              this.$message.error("你不可以修改会议数据")
              return
            }
            if (this.formData.datesType === '0') {
              this.$message.error("你不可以直接修改为会议日程")
              return

            }
            // 修改表单数据
            let form = {
              id: this.formData.id,
              datesTypeR: this.elRow,
              description: this.formData.description,
              title: this.formData.title,
              startTime: this.formData.startTime,
              endTime: this.formData.endTime,
              datesType: this.formData.datesType,
            }
            if (isNaN(form.datesType)) {
              console.log(form.datesType)
              this.dict.type.schedule_type.forEach(item => {
                if (item.label == this.formData.datesType) {
                  form.datesType = item.value
                }
              })
            }
            updateMine(form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.isAdd = false
            addMine(this.formData).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除我的日程编号为"' + ids + '"的数据项？').then(function () {
      }).then(() => {
        let datesType = this.elRows || row.datesType;
        if (datesType.length) {
          datesType.forEach(item => {
            if (item === 0) {
              this.$message.error("你不可以删除会议数据")
              return false
            }
          })
        }else {
          if(datesType === 0){
            this.$message.error("你不可以删除会议数据")
            return false
          }
        }
        return delMine(ids, datesType).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('management/mine/export', {
        ...this.queryParams
      }, `mine_${new Date().getTime()}.xlsx`)
    },
  }
}
</script>

